#!/bin/bash

# 第1步：询问用户输入要安装的Python版本
read -p "请输入要安装的Python版本（例如：3.8.5）: " PYTHON_VERSION

# 第2步：下载Python源码
echo "正在下载Python源码..."
wget 10.75.2.185:8080/group1/test/Python-${PYTHON_VERSION}.tgz
if [ $? -ne 0 ]; then
    echo "下载Python源码失败"
    exit 1
fi

# 第3步：创建安装目录
echo "正在创建安装目录..."
mkdir -p /usr/local/python3
if [ $? -ne 0 ]; then
    echo "创建安装目录失败"
    exit 1
fi

# 第4步：安装依赖
echo "正在安装依赖..."
yum -y install gcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel zlib*
if [ $? -ne 0 ]; then
    echo "安装依赖失败"
    exit 1
fi

# 第5步：解压Python源码
echo "正在解压Python源码..."
tar -zxf Python-${PYTHON_VERSION}.tgz
if [ $? -ne 0 ]; then
    echo "解压Python源码失败"
    exit 1
fi

# 第6步：进入Python源码目录
cd Python-${PYTHON_VERSION}

# 第7步：编译Python
echo "正在编译Python..."
./configure --prefix=/usr/local/python3
if [ $? -ne 0 ]; then
    echo "配置Python失败"
    exit 1
fi

make && make install
if [ $? -ne 0 ]; then
    echo "编译安装Python失败"
    exit 1
fi

# 第8步：创建软连接
echo "正在创建软连接..."
ln -sf /usr/local/python3/bin/python3 /usr/bin/python
ln -sf /usr/local/python3/bin/pip3 /usr/bin/pip

# 清理临时文件
cd ..
rm -rf Python-${PYTHON_VERSION} Python-${PYTHON_VERSION}.tgz

# 输出安装成功信息
echo "Python ${PYTHON_VERSION} 已成功安装到 /usr/local/python3 目录下。"
